package com.service.impl;

import com.bean.entity.Order;
import com.bean.entity.OrderDetail;
import com.bean.vo.OrderDetailVo;
import com.bean.vo.OrderVo;
import com.bean.vo.query.OrderQueryVo;
import com.dao.OrderDetailMapper;
import com.dao.OrderMapper;
import com.service.OrderService;
import com.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单业务逻辑实现
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Override
    public PageUtil<OrderVo> queryOrder(OrderQueryVo orderQueryVo, PageUtil<OrderVo> pa) throws Exception {
        //查询总记录数
        int count = orderMapper.selectCount(orderQueryVo);
        pa.setCount(count);
        //判断，查询结果是否为空
        if (count == 0) throw new Exception("未找到符合条件的数据！");
        //判断，查询结果总记录数是否大于当前页数
        if (pa.getCurrPage() > pa.getTotalPage()) pa.setCurrPage(pa.getTotalPage());
        //设置起始序号
        orderQueryVo.setPageCode(pa.getPageCode());
        orderQueryVo.setCurrPage(pa.getCurrPage());
        orderQueryVo.setPageSize(pa.getPageSize());
//        System.out.println(goodsQueryVo);
//        System.out.println(pa);
        //查询分页数据
        List<OrderVo> orderVos = orderMapper.selectByPage(orderQueryVo);
        pa.setItems(orderVos);

        return pa;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modifyOrder(Order order, List<OrderDetail> orderDetails) throws Exception {
        /* 修改数据 */
        //查询订单物品清单
        List<OrderDetailVo> orderDetailList = orderDetailMapper.select(new OrderDetail(order.getOrderId()));
        //
        Map<String,OrderDetail> map = new HashMap<>();
        //
        for (int i = 0; i < orderDetails.size(); i++) {
            if (orderDetails.get(i).getId() != null){
                map.put(orderDetails.get(i).getGoodsId(),orderDetails.get(i));
            }
        }
        //删除
        for (int i = 0; i < orderDetailList.size(); i++) {
            //判断是否需要删除
            if (map.containsKey(orderDetailList.get(i).getGoodsId())) continue;
            int delete = orderDetailMapper.delete(orderDetailList.get(i));
            if (delete == 0) throw new Exception("修改失败!");
        }
        //修改已有及添加记录
        for (int i = 0; i < orderDetails.size(); i++) {
            if (orderDetails.get(i).getGoodsId().equals("")) continue;
            if (orderDetails.get(i).getId() == null){
                orderDetails.get(i).setOrderId(order.getOrderId());
                int insert = orderDetailMapper.insert(orderDetails.get(i));
                if (insert == 0) throw new Exception("修改失败!");
                continue;
            }
            int update1 = orderDetailMapper.update(orderDetails.get(i));
            //判断结果
            if (update1 == 0) throw new Exception("修改失败！");
        }
        //修改数据
        int update = orderMapper.update(order);
        //判断结果
        if (update == 0) throw new Exception("修改失败！");

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrder(Order order, List<OrderDetail> orderDetail) throws Exception {
        //添加数据
        /* 验证物品信息非空 */
        for (int i = 0; i < orderDetail.size(); i++) {
            if(orderDetail.get(i).getGoodsId().equals("") || orderDetail.get(i).getGoodsId() == null)
                throw new Exception("物品名不能为空");
            if(orderDetail.get(i).getQuantity() == null) throw new Exception("物品数量不能为空");
            if(orderDetail.get(i).getUnit().equals("") || orderDetail.get(i).getUnit() == null)
                throw new Exception("物品计量单位不能为空");
        }
        /* 生成系统数据 */
        Date now = new Date();
        //创建时间
        order.setCreateDate(now);
        //订单编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        if (order.getOrderType() == 1){
            order.setOrderId("CP" + sdf.format(now));
        } else if (order.getOrderType() == 2){
            order.setOrderId("CL" + sdf.format(now));
        }
        //添加订单物品信息
        for (int i = 0; i < orderDetail.size(); i++) {
            orderDetail.get(i).setOrderId(order.getOrderId());
            int insert1 = orderDetailMapper.insert(orderDetail.get(i));
            //判断结果
            if (insert1 == 0) throw new Exception("添加失败！");
        }
        //添加数据
        int insert = orderMapper.insert(order);
        //判断结果
        if (insert == 0) throw new Exception("添加失败！");

        return 1;
    }

}
